package com.qf.hospital.controller;

import com.github.pagehelper.PageInfo;
import com.qf.hospital.dto.SysRoleDTO;
import com.qf.hospital.entity.SysRole;
import com.qf.hospital.enums.HospitalErrorEnums;
import com.qf.hospital.exception.HospitalException;
import com.qf.hospital.service.SysRoleService;
import com.qf.hospital.vo.LayUIDTreeVO;
import com.qf.hospital.vo.R;
import com.qf.hospital.vo.SysRoleVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @version V1.0
 * @Project hospital
 * @Package com.qf.hospital.controller
 * @Description: ${TODO}
 * @Date 2021/10/18 19:23
 */

@RestController
public class RoleController {

    private Logger log = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private SysRoleService roleService;

    @GetMapping("/role/loadAllRole")
    public R loadAllRole(String rolename, String roledesc, Integer available,
                         @RequestParam(defaultValue = "1")Integer page,
                         @RequestParam(defaultValue = "10")Integer limit){
        //1.调用service查询角色信息
        PageInfo<SysRole> pageInfo = roleService.loadAllRole(rolename,roledesc,available,page,limit);
        //2.封装数据
        long count = pageInfo.getTotal();
        List<SysRoleVO> data = new ArrayList<>();
        for (SysRole sysRole : pageInfo.getList()) {
            SysRoleVO vo = new SysRoleVO();
            BeanUtils.copyProperties(sysRole, vo);
            data.add(vo);
        }
        //3.返回
        return R.ok(count,data);
    }

    @PostMapping("/role/initRoleMenuTreeJson")
    public R initRoleMenuTreeJson(Integer roleid){

        if (roleid == null || roleid <= 0){
            log.info("【分配角色信息】 参数不合法! roleid = {}",roleid);
            throw new HospitalException(HospitalErrorEnums.REQUEST_PARAMETER_ERROR);
        }
        List<LayUIDTreeVO> data = roleService.initRoleMenuTreeJson(roleid);
        return R.ok(data);
    }

    @PostMapping("/role/saveRoleMenu")

    public R saveRoleMenu(Integer roleid,Integer[] ids){
        if (roleid == null){
            log.info("【分配角色信息】 参数不合法! ");
            return R.error(HospitalErrorEnums.REQUEST_PARAMETER_ERROR);
        }

        roleService.saveRoleMenu(roleid,ids);
        return R.ok("分配菜单成功！");
    }


    @PostMapping("/role/deleteRole")
    public R deleteRole(Integer roleid){
        if (roleid == null || roleid <= 0){
            log.info("【删除角色信息】 参数不合法! ");
            return R.error(HospitalErrorEnums.REQUEST_PARAMETER_ERROR);
        }
        roleService.deleteByRoleId(roleid);
        return R.ok("删除成功!");
    }


    @PostMapping("/role/updateRole")
    public R updateRole(@Valid SysRoleDTO roleDTO, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            String msg = bindingResult.getFieldError().getDefaultMessage();
            log.error("【修改角色信息】 参数不合法！", msg);
            return R.error(HospitalErrorEnums.REQUEST_PARAMETER_ERROR);
        }

        SysRole role = new SysRole();
        BeanUtils.copyProperties(roleDTO, role);
        roleService.updateByRoleId(role);
        return R.ok("修改成功!");
    }

    @PostMapping("/role/insertRole")
    public R insertRole(@Valid SysRoleDTO roleDTO, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            String msg = bindingResult.getFieldError().getDefaultMessage();
            log.error("【添加角色信息】 参数不合法！", msg);
            return R.error(HospitalErrorEnums.REQUEST_PARAMETER_ERROR);
        }

        SysRole role = new SysRole();
        BeanUtils.copyProperties(roleDTO, role);
        roleService.addRole(role);
        return R.ok("添加成功!");
    }
}
